package cn.com.self.apis.controller;


import cn.com.self.apis.files.entity.Files;
import cn.com.self.apis.portal.entity.AdvertisementEntity;
import cn.com.self.apis.portal.entity.NoticeEntity;
import cn.com.self.apis.portal.entity.RequirementsEntity;
import cn.com.self.apis.portal.service.intf.IAdvertisementService;
import cn.com.self.apis.portal.service.intf.IFileRelationService;
import cn.com.self.apis.portal.service.intf.INoticeService;
import cn.com.self.apis.portal.service.intf.IRequirementsService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "首页")
public class HomePageController {

    @Autowired
    IAdvertisementService advertisementService;
    @Autowired
    IFileRelationService fileRelationService;
    @Autowired
    IRequirementsService requirementsService;
    @Autowired
    INoticeService noticeService;

    @GetMapping(value = "/carouselAdver/page")
    @ApiOperation(value = "分页获取首页轮播图")
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort", "access_token");
        ICustomPage<AdvertisementEntity> pageData = this.advertisementService.page(queryParams, page);
        //封装文件对象
        for (AdvertisementEntity pageDatum : pageData.getRows()) {
            Long id = pageDatum.getId();
            List<Files> files = fileRelationService.findFilesByObjId(id);
            if (files != null) {
                pageDatum.setFiles(files);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    @GetMapping(value = "/requirements/page")
    @ApiOperation(value = "首页分页获取需求发布")
    public ResponseEntity gertRequirementsPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort","access_token");
        ICustomPage<RequirementsEntity> pageData = this.requirementsService.page(queryParams, page);
        for (RequirementsEntity pageDatum : pageData.getRows()) {
            Long id = pageDatum.getId();
            List<Files> files = fileRelationService.findFilesByObjId(id);
            if (files != null){
                pageDatum.setFiles(files);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }


    @GetMapping(value = {"/notice/load/{id}"})
    @ApiOperation(value = "根据id获取通知")
    public ResponseEntity loadId(@PathVariable("id") String id, HttpServletRequest request) {
        NoticeEntity noticeEntity = this.noticeService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(noticeEntity));
    }

    @GetMapping(value = "/notice/page")
    @ApiOperation(value = "分页获取通知")
    public ResponseEntity getNoticePage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort","access_token");
        ICustomPage<NoticeEntity> pageData = this.noticeService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }



}

